package com.panda.topic.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.panda.sys.entity.User;
import com.panda.topic.entity.Topic;
import com.panda.topic.entity.TopicAcceVo;
import com.panda.topic.service.TopicService;
import com.panda.utils.CommonUtils;
import com.panda.utils.Pager;
import com.panda.utils.contantUtil;

@Controller
public class TopicController {
	
	@InitBinder
	protected void initBinder(WebDataBinder binder){
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
	@Resource
	private TopicService topicService;
	
 
	@RequestMapping("/topic/list")
	public String list(){
		return "topic/list";
	}
 
	@RequestMapping("/topic/getlist")
	@ResponseBody
	public String getlist(Model model,Long offSet,Long pageSize,String name,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		Pager<Topic> topics=topicService.findListLimit(offSet, pageSize,user.getId(),name);
		return JSON.toJSONString(topics);
	}
	
 
	@RequestMapping("/topic/findById")
	@ResponseBody
	public String findById(Long id){
		Topic topic=topicService.findById(id);
		return CommonUtils.msgCallback(true,"查询成功",null, JSON.toJSONString(topic));
	}
 
	@RequestMapping("/topic/save")
	@ResponseBody
	public String save(Topic topic, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		topic.setCreator(user.getId());
		topic.setCreatorName(user.getName());
		topicService.saveOrUpdate(topic);
		return JSON.toJSONString(true);
	}
	
	@ResponseBody
	@RequestMapping("/topic/delete")
	public String delete(Long id){
		topicService.delete(id);
		return CommonUtils.msgCallback(true,"删除成功",null, null);
	}
	
	@ResponseBody
	@RequestMapping("/topic/addAcce")
	public String addAcce(Long tid,Long fid){
		topicService.addAcce(tid,fid);
		return CommonUtils.msgCallback(true,"保存成功",null, null);
	}
	
	@ResponseBody
	@RequestMapping("/topic/delAcce")
	public String delAcce(Long taid){
		topicService.delAcce(taid);
		return CommonUtils.msgCallback(true,"删除成功",null, null);
	}
	
	@ResponseBody
	@RequestMapping("/topic/findAcce")
	public String findAcce(Long tid){
		List<TopicAcceVo> list=topicService.findAcce(tid);
		return CommonUtils.msgCallback(true,"查询成功",null, JSON.toJSONString(list));
	}
	
}
